package cn.hn.tx.rebot.task.job;

import java.util.Iterator;
import java.util.List;

import cn.hn.comm.Tools;
import cn.hn.tx.rebot.poll.model.RebotPoll;
import cn.hn.tx.rebot.poll.service.ICrawlPollService;
import cn.hn.tx.rebot.poll.service.IRebotPollService;
import cn.hn.tx.rebot.task.model.TaskExeLog;

/**
 * 抓取话题
 * @author shenhao
 *
 */
public class Task_CrawlPoll_DiscuzX2 extends ITaskJob {
	
	private ICrawlPollService crawlPollService; 
	private IRebotPollService rebotPollService;
	
	private String url;
	
	private TaskType taskType;
	
	@Override
	void executeJob() {
		System.out.println("开始:" + getTaskName());
		// 任务执行记录日志
		TaskExeLog taskExeLog = new TaskExeLog();
		taskExeLog.setStartTime(Tools.getCurDate(null));
		taskExeLog.setTaskName(this.getTaskName());
		taskExeLog.setType(type().getType());
		String taskLogMemo;

		try {
			int iRebotPollCount = 0;
			// 获取列表中的话题
			List<RebotPoll> listRebotPolls = crawlPollService.getRebotPolls(url);
			if (listRebotPolls != null) {
				// 迭代话题
				Iterator<RebotPoll> iterRebotPoll = listRebotPolls.iterator();
				while (iterRebotPoll.hasNext()) {
					RebotPoll rebotPoll = iterRebotPoll.next();
					
					
					// 话题写入
					rebotPollService.insertRebotPoll(rebotPoll);
//					rebotBlogService.insertRebotWriteBlog_HandleHref("小三",
//							"http://www.tianxin007.com", blog.getTitle(), blog
//									.getContent(), blog.getUrl(), crawl_type,
//							Tools.getCurDate(null), null, 0);
					iRebotPollCount++;
				}
			}

			taskLogMemo = "抓取话题:" + iRebotPollCount;
		} catch (Exception e) {
			// 记录异常
			getExceptionService().insertException(this.getClass().getName(),
					"executeJob()", e.toString());
			taskLogMemo = "任务执行失败.原因:" + e.toString();
		}
		taskExeLog.setMemo(taskLogMemo);
		taskExeLog.setEndTime(Tools.getCurDate(null));
		// 记录任务执行日志
		getTaskService().insertTaskExeLog(taskExeLog);
		System.out.println("结束:" + getTaskName());
	}

	@Override
	public
	TaskType type() {
		return taskType;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public TaskType getTaskType() {
		return taskType;
	}

	public void setTaskType(TaskType taskType) {
		this.taskType = taskType;
	}
	
	

}
